<template>
    <view>
        <view class="content">
            <!-- 顶部导航栏 -->
            <Navbar></Navbar>
            <!-- 
                1 开启下拉刷新 refresher-enabled
                2 下拉事件 refresherrefresh
                3 滚动条触底事件 scrolltolower
                4 运行上下滚动  scroll-y 
            -->
            <scroll-view scroll-y class="main" @scrolltolower="scrolltolower" refresher-enabled
                @refresherrefresh="refresherrefresh" :refresher-triggered="refresherTriggered">
                <!-- 轮播图 -->
                <Carousel :banners="banners" height="280rpx"></Carousel>
                <!-- 前台类目 -->
                <CateScroll :categoryHeadMutli="categoryHeadMutli"></CateScroll>
                <!-- 热门推荐 -->
                <view class="panel recommend">
                    <view class="item" v-for="item in hotMutli" :key="item.id">
                        <view class="title">
                            {{ item.title }}<text>{{ item.alt }}</text>
                        </view>
                        <navigator hover-class="none" :url="`/pages/recommend/index?type=${item.type}`" class="cards">
                            <image mode="aspectFit" v-for="img in item.pictures" :key="img" :src="img"></image>
                        </navigator>
                    </view>
                </view>
                <!-- 新鲜好物 -->
                <view class="panel fresh">
                    <view class="title">
                        新鲜好物
                        <navigator hover-class="none" class="more" url="/pages/recommend/index?type=5">更多</navigator>
                    </view>
                    <view class="cards">
                        <navigator hover-class="none" :url="`/pages/goods/index?id=${item.id}`" v-for="item in homeNew"
                            :key="item.id">
                            <image mode="aspectFit" :src="item.picture"></image>
                            <view class="name">{{ item.name }}</view>
                            <view class="price">
                                <text class="small">¥</text>{{ item.price }}
                            </view>
                        </navigator>
                    </view>
                </view>
                <!-- 热门品牌 -->
                <view class="panel brands">
                    <view class="title">
                        热门品牌
                        <navigator hover-class="none" class="more" url="/pages/list/index">更多</navigator>
                    </view>
                    <view class="cards">
                        <navigator hover-class="none" url="/pages/goods/index">
                            <image mode="aspectFit"
                                src="http://static.botue.com/erabbit/static/uploads/brand_logo_1.jpg">
                            </image>
                            <view class="name">小米</view>
                            <view class="price">99元起</view>
                        </navigator>
                        <navigator hover-class="none" url="/pages/goods/index">
                            <image mode="aspectFit"
                                src="http://static.botue.com/erabbit/static/uploads/brand_logo_2.jpg">
                            </image>
                            <view class="name">TCL</view>
                            <view class="price">199起</view>
                        </navigator>
                        <navigator hover-class="none" url="/pages/goods/index">
                            <image mode="aspectFit"
                                src="http://static.botue.com/erabbit/static/uploads/brand_logo_3.jpg">
                            </image>
                            <view class="name">饭小宝</view>
                            <view class="price">9.9起</view>
                        </navigator>
                        <navigator hover-class="none" url="/pages/goods/index">
                            <image mode="aspectFit"
                                src="http://static.botue.com/erabbit/static/uploads/brand_logo_4.jpg">
                            </image>
                            <view class="name">鳄鱼</view>
                            <view class="price">299起</view>
                        </navigator>
                    </view>
                </view>
                <!-- 猜你喜欢 -->
                <Guess :homeGoodsGuessLike="goodsGuessLike"></Guess>
            </scroll-view>
        </view>
    </view>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import Navbar from './components/Navbar.vue'
//引入发送请求的函数
import { getHomeBanner, getHomeCategoryMutli, getHomeHotMutli, getHomeNew, getHomeGoodsGuessLike } from '@/api/home'

export default {
    components: {
        Navbar
    },
    data() {
        return {
            //轮播图数据
            banners: [],//数组
            //前台类目
            categoryHeadMutli: [],
            //热门推荐
            hotMutli: [],
            //新鲜好物
            homeNew: [],
            //猜你喜欢
            goodsGuessLike: [],
            //下拉刷新状态
            refresherTriggered: false

        }
    },
    onLoad() {
        //轮播图
        this.getHomeBanner()
        //前台类目
        this.getHomeCategoryMutli()
        //热门推荐
        this.getHomeHotMutli()
        //新鲜好物
        this.getHomeNew()
        //猜你喜欢
        this.GuessParams = {
            page: 1,
            pageSize: 10
        }
        //添加一个 猜你喜欢的节流开关
        this.isLoadding = false;
        // 总页数
        this.GuessPages = 1;
        //猜你喜欢请求
        this.getHomeGoodsGuessLike()
    },
    computed: {

    },
    methods: {
        //首页广告位轮播图
        async getHomeBanner() {
            const res = await getHomeBanner()
            // console.log(res);
            this.banners = res.result
        },
        //前台分类
        async getHomeCategoryMutli() {
            const res = await getHomeCategoryMutli()
            // console.log(res);
            this.categoryHeadMutli = res.result
        },
        //热门推荐
        async getHomeHotMutli() {
            const res = await getHomeHotMutli()
            // console.log(res);
            this.hotMutli = res.result
        },
        //新鲜好物
        async getHomeNew() {
            const res = await getHomeNew()
            // console.log(res);
            this.homeNew = res.result
        },
        //猜你喜欢
        async getHomeGoodsGuessLike() {
            const res = await getHomeGoodsGuessLike(this.GuessParams)
            console.log(res);
            // this.goodsGuessLike = res.result.items; // 修改为 数组追加
            this.goodsGuessLike = [...this.goodsGuessLike, ...res.result.items]; // 修改为 数组追加

            //获取总页数
            this.GuessPages = res.result.pages


        },
        //分页事件中的逻辑 scroll-view 滚动到底部触发 分页加载
        async scrolltolower() {
            /*
                分页关键 page=1  page++ page=2 page=3
                1 判断还有没有下一页！！ （拿当前页码 和 总页数做比较即可 ）
                2 有-
                  1 page++
                  2 发送请求 获取最新的一页数据
                  3 新的数据回来，要追加到 旧的数组中
                3 优化
                  1 节流的效果
                    这一次的请求没有完成，不让你开启下一次的请求

            */
           /* 
           正常执行的判断逻辑是
           1.没有人发请求
           2.分页判断 当前页数<总页数

           */
            if (!this.isLoadding && this.GuessParams.page < this.GuessPages) {
                this.isLoadding = true;
                // 还有下一页数据
                this.GuessParams.page++;
                await this.getHomeGoodsGuessLike(); // 异步代码
                // 请求完成了 数据渲染完毕
                this.isLoadding = false;
            }
        },
        //scroll-view 下拉刷新事件
        async refresherrefresh() {
            /* 清空旧数据
               1.data中的数据
               2.一些全局变量  分页 页码
               获取新的数据 
            */

            //清空
            //轮播图数据
            this.banners = []//数组
            //前台类目
            this.categoryHeadMutli = []
            //热门推荐
            this.hotMutli = []
            //新鲜好物
            this.homeNew = []
            //猜你喜欢
            this.goodsGuessLike = []
            //一些全局变量  分页 页码
            this.GuessParams.page = 1
            this.GuessPages = 1
            // this.isLoadding = false

            //获取新的数据
            await Promise.all([
                //轮播图
                this.getHomeBanner(),
                //前台类目
                this.getHomeCategoryMutli(),
                //热门推荐
                this.getHomeHotMutli(),
                //新鲜好物
                this.getHomeNew(),
                //猜你喜欢请求
                this.getHomeGoodsGuessLike(),
            ])

            //模拟 数据都请求回来
            this.refresherTriggered = false


            // this.refresherTriggered = true; // 开启下拉状态
            // setTimeout(()=>{
            //     //模拟 数据都请求回来
            //     this.refresherTriggered=false
            // },2000)
        }
    }
};
</script>

<style lang="scss" scoped>
.content {
    height: 100vh;
    display: flex;
    flex-direction: column;
}

.main {
    flex: 1;
    overflow: hidden;
    background-color: #f7f7f8;
}

/* 公共面板 */
.panel {
    margin: 20rpx 20rpx 0;
    border-radius: 10rpx;
    background-color: #fff;

    .title {
        display: flex;
        align-items: center;
        padding: 24rpx 24rpx 0;
        font-size: 32rpx;
        color: #262626;
        position: relative;

        text {
            font-size: 24rpx;
            color: #7f7f7f;
            margin-left: 18rpx;
        }
    }

    .more {
        position: absolute;
        right: 24rpx;
        font-size: 26rpx;
        color: #7f7f7f;
    }

    .cards {
        display: flex;
    }

    .name {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
}

/* 推荐专区 */
.recommend {
    display: flex;
    flex-wrap: wrap;

    .item {
        display: flex;
        flex-direction: column;
        width: 50%;
        height: 254rpx;
        border-right: 1rpx solid #eee;
        border-top: 1rpx solid #eee;

        &:nth-child(2n) {
            border-right: 0 none;
        }

        &:nth-child(-n + 2) {
            border-top: 0 none;
        }
    }

    .cards {
        flex: 1;
        padding: 15rpx 20rpx;
        justify-content: space-between;

        image {
            width: 48%;
        }
    }
}

/* 新鲜好物 */
.fresh {
    min-height: 320rpx;

    .price {
        line-height: 1;
        text-align: left;
        font-size: 26rpx;
        color: #cf4444;
    }

    .small {
        font-size: 80%;
    }
}

.fresh .cards navigator,
.brands .cards navigator {
    width: 25%;
    padding: 15rpx 20rpx 20rpx;
    line-height: 1;
    text-align: center;
}

.fresh image,
.brands image {
    width: 126rpx;
    height: 126rpx;
}

.fresh .name,
.brands .name {
    margin: 20rpx 0 16rpx;
    font-size: 24rpx;
    color: #262626;
}

.brands {
    .price {
        font-size: 24rpx;
        color: #999;
    }
}
</style>
